<?php 
class kid_recent_post_widget extends WP_Widget {
 
    function kid_recent_post_widget() {
        $widget_ops = array( 'classname' => 'recent_post-widget','description' => 'Những bài viết mới nhất' );
        $control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'recent_post-widget' );
        $this->WP_Widget( 'recent_post-widget','Kid - Recent_post', $widget_ops, $control_ops );
    }
     
    function widget( $args, $instance ) {
        extract( $args );
 
        $title = apply_filters('widget_title', $instance['title'] );
        if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) )
 			$number = 10;
        $r = new WP_Query( apply_filters( 'widget_posts_args', array( 'posts_per_page' => $number, 'no_found_rows' => true, 'post_status' => 'publish', 'ignore_sticky_posts' => true,'post_type'=> array('audios','videos'))));
		if ($r->have_posts()) :
        echo $before_widget;
        if ( $title )
            echo $before_title;
            echo $title ; ?>
        <?php echo $after_title;
            global $wpdb, $post;?>
              <div class="in-sec in-sec-nopad">
            <!-- Timeline Start -->
            <div id="tab-timeline">
                <ul class="timeline">
                    <?php while ( $r->have_posts() ) : $r->the_post(); ?>
                            <li>
                                <div class="date">
                                    <span>&nbsp;</span>
                                    <h6 class="colr">
                                        <?php echo get_the_date(); ?>
                                    </h6>
                                </div>
                                <div class="desc">
                                    <div class="desc-in">
                                        <span class="pointer">&nbsp;</span>
                                        <div class="thumb">
                                            <a href="<?php echo get_permalink()?>">
                                                <?php
                                                    $image_id = get_post_thumbnail_id ( $post->ID );
                                                    if ( $image_id <> "" ) {
                                                        $image_url = cs_attachment_image_src($image_id, 120, 95);
                                                        echo "<img src='".$image_url."' />";
                                                    }
                                                    else {
                                                        echo "<img width='120' height='95' src='".get_template_directory_uri()."/images/no_image.jpg' />";
                                                    }
                                                ?>	
                                            </a>
                                        </div>
                                        <div class="txt">
                                            <h5>
                                                <a href="<?php echo get_permalink()?>">
                                                    <?php echo substr(get_the_title(), 0, 50);  if ( strlen(get_the_title()) > 50 ) echo "..."; ?>
                                                </a>
                                            </h5>
                                            <p>
                                                <?php
                                                    $get_the_excerpt =$post->post_content ;
                                                        $get_the_excerpt = trim(preg_replace('/<a[^>]*>(.*)<\/a>/iU','', $get_the_excerpt ));
                                                        echo substr( get_the_excerpt(), 0, strrpos( substr( get_the_excerpt(), 0,400), ' ' ) );
                                                        if ( strlen( $get_the_excerpt ) > 50 ) {
                                                            echo ' ... <a href="'.get_permalink().'">xem bài này</a>';
                                                        }
                                                ?>
                                            </p>
                                        </div>
                                     　</div>
                                    </div>
                                </li>
                    <?php
                        endwhile;  
                    ?>
                </ul>
                        <div class="clear"></div>
                            <?php
                            // pagination start
                                if ( $node->cs_event_pagination == "Show Pagination" and $node->cs_event_per_page > 0 and $node->cs_event_filterables == "No" ) {
                                    echo "<div class='in-sec'><ul class='pagination'>";
                                        $qrystr = '';
                                        if ( isset($_GET['page_id']) ) $qrystr = "&page_id=".$_GET['page_id'];
                                        if ( isset($_GET['filter_category']) ) $qrystr .= "&filter_category=".$_GET['filter_category'];
                                    echo cs_pagination($count_post, $node->cs_event_per_page, $qrystr);
                                    echo "</ul></div>";
                                }
                            // pagination end
                            ?>
                    </div>
                    <!-- Timeline End -->
            <div class="clear"></div>
        </div>
		<?php echo $after_widget; ?>
    
 <?php
        wp_reset_postdata();
	endif;
    }
    function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $instance['title'] = strip_tags( $new_instance['title'] );
        $instance['number'] = strip_tags( $new_instance['number'] );
        return $instance;
    }
 
    function form( $instance ) {
        $defaults = array( 'title' =>__( 'Recent Post' , 'tie') );
        $instance = wp_parse_args( (array) $instance, $defaults ); ?>
 
        <p>
            <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title : </label>
            <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" type="text" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id( 'number' ); ?>">Number Item : </label>
            <input id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" value="<?php echo $instance['number']; ?>" class="widefat" type="text" />
        </p>
 
 
    <?php
    }
}
add_action( 'widgets_init', 'recent_post_widget_box' );
function recent_post_widget_box() {
    register_widget( 'kid_recent_post_widget' );
}